package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.service.LoginService;
import com.project.entity.Login;
import com.project.entity.UserMember;
import com.project.mapper.LoginMapper;
import com.project.service.UserMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements LoginService {
    @Autowired
    private LoginService loginService;
    @Autowired
    private UserMemberService userMemberService;
    //员工登陆后保存到login
    public Login save(UserMember user){
        //如login中已经有记录，直接修改登陆时间
        LambdaQueryWrapper<Login> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Login::getUserId,user.getUserId());
        Login login1 = loginService.getOne(queryWrapper);
        if(login1!=null){
            login1.setLoginTime(LocalDateTime.now());
            loginService.updateById(login1);
            return login1;
        }
        else {
            Login login=new Login();
            login.setName(user.getName());
            login.setUserId(user.getUserId());
            login.setIdentity(user.getIdentity());
            login.setPassword(user.getPassword());
            login.setModifiedPassword(user.getPassword());
            login.setAgainModifiedPassword(user.getPassword());


                login.setPhone(user.getPhone());



            login.setLoginTime(LocalDateTime.now());
            login.setModifiedTime(LocalDateTime.now());
            loginService.save(login);
            return login;
        }

    }
}
